package com.example.xymarket.adapter;

import android.content.Context;

import com.example.xymarket.adapter.holder.MessengerViewHolder;
import com.example.xymarket.common.ChatConfig;
import com.example.xymarket.R;

import android.os.Handler;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MessengerAdapter extends RecyclerView.Adapter<MessengerViewHolder>{


    private List<ChatConfig.chatUser> chatUserList;
    private Handler handler;
    private Context context;

    public MessengerAdapter(List<ChatConfig.chatUser> chatUserList, Handler handler,Context context) {
        this.chatUserList = chatUserList;
        this.handler = handler;
        this.context=context;
    }

    @NonNull
    @Override
    public MessengerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MessengerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_messenger, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MessengerViewHolder holder, int position) {
        ChatConfig.chatUser chatUser=chatUserList.get(position);
        holder.textView.setText(chatUser.name);
        holder.imageView.setImageResource(chatUser.avatar);
        holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0);
            }
        });
    }

    @Override
    public int getItemCount() {
        return chatUserList.size();
    }



    @Override
    public long getItemId(int position) {
        return position;
    }

    public void add(ChatConfig.chatUser chatUser) {
        chatUserList.add(0,chatUser);
        notifyItemInserted(0);
    }
}
